<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class School_video_user_code extends MY_Controller {
	function __construct(){
		parent::__construct();
		
	}
	function index($school_id = 0, $limit = 0){
		$this->load->model("Schools");
		$result = $this->Schools->get_record_list($limit, $this->offset, $conditions);
		$this->load->library('pagination');
		$page_config['base_url'] = '/school_video_user_code/index/' . $school_id;
		$page_config['total_rows'] = $result['count'];
		$page_config['per_page'] = $this->offset;
		$page_config['uri_segment'] = 6;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();

		$this->load->model("Agents");
		$agent_list = $this->Agents->get_agent_list();
		$this->smarty->assign("action", '网校列表');
		$this->smarty->assign("conditions", $conditions);
		$this->smarty->assign("current_url", $current_url);
		$this->smarty->assign("school_list", $result['result']);
		$this->smarty->assign("agent_list", $agent_list);
		$this->smarty->assign("pagination", $pagination);
		$this->smarty->display("school/list.html");
	}

	function add($school_id = 0){
		$post_data = $this->input->post();
		if(empty($post_data)){
			$this->load->model('Goods_courses');
			$school = $this->Goods_courses->get_school();
			$this->smarty->assign("school", $school);
			$this->smarty->assign("school_id", $school_id);
			$this->smarty->assign("action", "授权网校视频课用户");
			$this->smarty->display("school_video_user_code/add.html");
		}else{
			if($post_data['num'] > 0){
				$expire_time = strtotime($post_data['expire_time']);
				$record_data['school_id'] = $record_data['data_id'] = $post_data['school_id'];
				$record_data['data_type'] = 6;
				$record_data['type'] = 1;
				$record_data['add_time'] = time();
				$record_data['reviewed'] = 1;
				$record_data['num'] = $post_data['num'];
				$record_data['remarks'] = $post_data['remarks'];
				$record_data['expire_day'] = $expire_time;
				$this->db->insert('o2o_school_order_records',$record_data);
				$record_id = $this->db->insert_id();
				$num = $post_data['num'];
				$time = time();
				for ($i=0; $i < $num; $i++) { 
					$yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
					$code = $yCode[rand(0, 9)] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
					$code_data[] = array("school_id" => $post_data['school_id'], "code" => "CV" . $code, "add_time" => $time, "expire_time" => $expire_time, "record_id" => $record_id);
				}
				$insert_result = $this->db->insert_batch("o2o_school_video_user_codes", $code_data);

				$this->db->where("id", $post_data['school_id']);
				$this->db->set("item_num", "item_num + {$num}", false);
				$this->db->update("o2o_school");

			}else{
				echo "激活码数量不能为空！";exit;
			}

			if($record_id > 0){
				show_message($_SERVER['HTTP_REFERER'],'添加成功');
			}else{
				show_message($_SERVER['HTTP_REFERER'],'操作失败');
			}
		}
	}
}